package com.smarth.zhizulian.http.observer.bad;

import android.content.Context;

import com.smarth.zhizulian.http.observer.inter.BaseObserver;
import com.smarth.zhizulian.view.LoadingLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by 18865 on 2017/12/23.
 * 列表分页刷新访问
 */

public abstract class BadRefreshObserver<T> extends BaseObserver<T> {
    private SmartRefreshLayout mRefreshView;
    private LoadingLayout mLoadingLayout;
    private List mData;

    public BadRefreshObserver(Context context, SmartRefreshLayout refreshView, LoadingLayout loadingLayout, List data) {
        super(context);
        this.mRefreshView = refreshView;
        this.mLoadingLayout = loadingLayout;
        this.mData = data;
    }

    public BadRefreshObserver(Context context, SmartRefreshLayout refreshView, LoadingLayout loadingLayout) {
        super(context);
        this.mRefreshView = refreshView;
        this.mLoadingLayout = loadingLayout;
        List<String> list = new ArrayList<>();
        list.add("");
        this.mData = list;
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        super.onSubscribe(d);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        super.onError(e);
        if (mRefreshView != null) {
            if (mRefreshView.isRefreshing()) {
                mRefreshView.finishRefresh();
            } else {
                mRefreshView.finishLoadmore();
            }
        }
        if (mLoadingLayout != null) {
            if (mData != null && mData.size() > 0) {
                mLoadingLayout.showContent();
            } else {
                mLoadingLayout.showError();
            }
        }

    }

    @Override
    public void onComplete() {
        if (mRefreshView != null) {
            if (mRefreshView.isRefreshing()) {
                mRefreshView.finishRefresh();
            } else {
                mRefreshView.finishLoadmore();
            }
        }
        if (mLoadingLayout != null) {
            if (mData != null && mData.size() == 0) {
                mLoadingLayout.showEmpty();
            } else {
                mLoadingLayout.showContent();
            }
        }
    }

}

